package cn.edu.tju.elm.service;

import cn.edu.tju.elm.model.Business;
import cn.edu.tju.elm.model.Food;
import cn.edu.tju.elm.repository.BusinessRepository;
import cn.edu.tju.elm.repository.FoodRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class FoodService {
    @Autowired
    private FoodRepository foodRepository;
    
    @Autowired
    private BusinessRepository businessRepository;
    
    /**
     * 获取所有食品
     * @return 食品列表
     */
    public List<Food> getAllFoods() {
        return foodRepository.findAll();
    }
    
    /**
     * 根据商家ID获取食品列表
     * @param businessId 商家ID
     * @return 食品列表
     */
    public List<Food> getFoodsByBusinessId(Long businessId) {
        List<Food> allFoods = foodRepository.findAll();
        return allFoods.stream()
                .filter(food -> food.getBusiness() != null && food.getBusiness().getId().equals(businessId))
                .collect(Collectors.toList());
    }
    
    /**
     * 根据ID获取食品
     * @param id 食品ID
     * @return 食品对象
     */
    public Optional<Food> getFoodById(Long id) {
        return foodRepository.findById(id);
    }
    
    /**
     * 保存食品
     * @param food 食品对象
     * @return 保存后的食品对象
     */
    public Food saveFood(Food food) {
        // 验证必要字段
        if (food.getFoodName() == null || food.getFoodName().trim().isEmpty()) {
            throw new IllegalArgumentException("食品名称不能为空");
        }
        if (food.getFoodPrice() == null || food.getFoodPrice().compareTo(java.math.BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("食品价格必须大于0");
        }
        
        // 如果business只有ID，需要填充完整的Business对象
        if (food.getBusiness() != null && food.getBusiness().getId() != null) {
            Optional<Business> businessOpt = businessRepository.findById(food.getBusiness().getId());
            if (businessOpt.isPresent()) {
                food.setBusiness(businessOpt.get());
            } else {
                throw new IllegalArgumentException("指定的商家不存在");
            }
        } else {
            throw new IllegalArgumentException("商家不能为空");
        }
        return foodRepository.save(food);
    }
    
    /**
     * 更新食品信息
     * @param id 食品ID
     * @param foodDetails 更新的食品信息
     * @return 更新后的食品对象
     */
    public Food updateFood(Long id, Food foodDetails) {
        Optional<Food> food = foodRepository.findById(id);
        if (food.isPresent()) {
            Food existingFood = food.get();
            existingFood.setFoodName(foodDetails.getFoodName());
            existingFood.setFoodExplain(foodDetails.getFoodExplain());
            existingFood.setFoodImg(foodDetails.getFoodImg());
            existingFood.setFoodPrice(foodDetails.getFoodPrice());
            existingFood.setRemarks(foodDetails.getRemarks());
            if (foodDetails.getBusiness() != null) {
                Optional<Business> business = businessRepository.findById(foodDetails.getBusiness().getId());
                business.ifPresent(existingFood::setBusiness);
            }
            return foodRepository.save(existingFood);
        }
        return null;
    }
    
    /**
     * 删除食品
     * @param id 食品ID
     * @return 是否删除成功
     */
    public boolean deleteFood(Long id) {
        Optional<Food> food = foodRepository.findById(id);
        if (food.isPresent()) {
            foodRepository.deleteById(id);
            return true;
        }
        return false;
    }
}